/**
 * Notes: 基础项目服务类
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2023-08-30 19:20:00
 */

const BaseService = require('../../../framework/platform/service/base_service.js');
const util = require('../../../framework/helpers/util.js');
const timeUtil = require('../../../framework/helpers/time_util.js');
const dataUtil = require('../../../framework/helpers/data_util.js');
const cloudUtil = require('../../../framework/cloud/cloud_util.js');

class BaseProjectService extends BaseService {

	/** 构造函数 */
	constructor() {
		super();
	}

	/**
	 * 格式化排序字段
	 * @param {*} orderBy 
	 */
	getOrderByStr(orderBy) {
		if (!util.isDefined(orderBy) || !orderBy) return '';

		if (orderBy.indexOf('enroll_time') > -1) {
			// 按报名时间排序为创建时间
			return orderBy.replace(/enroll_time/g, this.getJoinField('EDIT_TIME'));
		}

		if (orderBy.indexOf('last_modified') > -1) {
			// 按最近修改时间排序为修改时间
			return orderBy.replace(/last_modified/g, this.getJoinField('EDIT_TIME'));
		}

		return orderBy;
	}

	/**
	 * 获取数据列表
	 * @param {*} service 
	 * @param {*} where 
	 * @param {*} options 
	 */
	async getListOfModel(service, where, options = {}) {
		let model = options.model || this.getModel();
		
		let orderBy = this.getOrderByStr(options.orderBy);
		
		let fields = options.fields || '*';
		let page = options.page || 1;
		let size = options.size || 10;
		let isTotal = options.isTotal || false;
		let oldTotal = options.oldTotal || 0;
		let totalFields = options.totalFields || '*';

		if (page > 1 && oldTotal > 0) {
			let total = oldTotal;
			let offset = 0;
			
			if (size > 0) offset = (page - 1) * size;
			
			let list = await model.getList(where, fields, orderBy, offset, size);
			
			return { list, total, page, size, oldTotal };
		} else {
			let offset = 0;
			
			if (size > 0) offset = (page - 1) * size;
			
			let list = await model.getList(where, fields, orderBy, offset, size);
			
			let total = 0;
			
			if (isTotal) {
				let totalRes = await model.getCount(where, totalFields);
				total = totalRes;
			}
			
			return { list, total, page, size };
		}
	}

	/**
	 * 获取单条数据
	 * @param {*} service 
	 * @param {*} id 
	 * @param {*} fields 
	 */
	async getOneOfModel(service, id, fields) {
		let model = this.getModel();
		
		let where = {
			_id: id
		};
		
		return await model.getOne(where, fields || '*');
	}

	/**
	 * 插入数据
	 * @param {*} service 
	 * @param {*} data 
	 */
	async insertOfModel(service, data) {
		let model = this.getModel();
		return await model.insert(data);
	}

	/**
	 * 修改数据
	 * @param {*} service 
	 * @param {*} id 
	 * @param {*} data 
	 */
	async editOfModel(service, id, data) {
		let model = this.getModel();
		
		let where = {
			_id: id
		};
		
		return await model.edit(where, data);
	}

	/**
	 * 删除数据
	 * @param {*} service 
	 * @param {*} id 
	 */
	async delOfModel(service, id) {
		let model = this.getModel();
		
		let where = {
			_id: id
		};
		
		return await model.del(where);
	}

	/**
	 * 高级状态修改
	 * @param {*} service 
	 * @param {*} id 
	 * @param {*} status 
	 */
	async statusOfModel(service, id, status) {
		let model = this.getModel();
		
		let where = {
			_id: id
		};
		
		let data = {
			EDIT_TIME: timeUtil.time(),
			STATUS: status
		};
		
		return await model.edit(where, data);
	}

	/**
	 * 添加研发项目
	 * @param {Object} data 项目数据
	 */
	async insertResearch(data) {
		// 使用云数据库的研发项目集合
		const cloud = cloudUtil.getCloud();
		const db = cloud.database();
		const collection = db.collection('research');
		
		// 插入数据
		let result = await collection.add({
			data
		});
		
		return {
			id: result._id
		};
	}
}

module.exports = BaseProjectService; 